/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.config;


import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.core.AprLifecycleListener;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
 * 设置tomcat运行模式
 * @author joewee
 * @version 1.0.0
 * @date 2018/10/11 10:06
 */
@EnableConfigurationProperties({TomcatServerProperties.class})
@Configuration
@Slf4j
public class TomcatServletProtocolWebServerFactoryCustomizer implements  Ordered {
    private final TomcatServerProperties tomcatServerProperties;

    public TomcatServletProtocolWebServerFactoryCustomizer(TomcatServerProperties tomcatServerProperties) {
        this.tomcatServerProperties = tomcatServerProperties;
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatServerProperties.Tomcat tomcatProperties = this.tomcatServerProperties.getTomcat();
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        String protocol = tomcatProperties.getProtocol();
        if (protocol != null) {
            protocol = protocol.toUpperCase();
            switch (protocol) {
                case "BIO":
                    tomcat.setProtocol("org.apache.coyote.http11.Http11Protocol");
                    log.warn("tomcat connector is running bio protocol,maybe the nio protocol is better");
                    break;
                case "NIO":
                    tomcat.setProtocol("org.apache.coyote.http11.Http11NioProtocol");
                    break;
                case "APR":
                    tomcat.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
                    break;
                default:
                    tomcat.setProtocol("org.apache.coyote.http11.Http11NioProtocol");
            }
        }
        tomcat.addContextLifecycleListeners(new AprLifecycleListener());
        return tomcat;
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
